package com.yys.gulimall.order.interceptor;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * @author zq
 * @version 1.0
 * @date 2021/4/3 15:41
 */
@Configuration
@Slf4j
public class FeignInterceptor {

    @Bean
    public RequestInterceptor requestInterceptor() {
        return requestTemplate -> {
            // threadlocal放入reqeust
            ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            if(requestAttributes != null) {
                // 同步请求头
                HttpServletRequest request = requestAttributes.getRequest();
                requestTemplate.header("Cookie",request.getHeader("Cookie"));
                log.info("feign远程调用添加拦截器");
            }
        };
    }
}
